<template>
  <el-drawer class="drawer-container" :visible.sync="visible" :title="title" append-to-body size="400px">
    <el-form ref="form" :model="formdata" :rules="formrule" label-position="right" label-suffix=":" label-width="100px">
      <el-form-item label="字典标签" prop="name">
        <el-input v-model="formdata.name" placeholder="请输入字典标签"/>
      </el-form-item>
      <el-form-item label="字典值" prop="code">
        <el-input v-model="formdata.code" :readonly="editable" placeholder="请输入字典值"/>
      </el-form-item>
      <el-form-item label="字典状态" prop="status">
        <el-radio-group v-model="formdata.status">
          <el-radio-button size="mini" v-for="item in localDict.system_status" :key="item.value" :label="item.value">
            {{ item.label }}
          </el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="序号" prop="sort">
        <el-input-number v-model="formdata.sort" placeholder="请输入字典序号"/>
      </el-form-item>
      <el-form-item label="字典描述">
        <el-input v-model="formdata.remark" :rows="3" placeholder="请输入字典描述" type="textarea"/>
      </el-form-item>
    </el-form>
    <div class="drawer-footer">
      <el-button size="mini" :loading="loading" type="primary" @click="handleSubmit">确定</el-button>
      <el-button size="mini" @click="handleCancel">取消</el-button>
    </div>
  </el-drawer>
</template>

<script>

import {getOne, save} from "@/api/base";

export default {
  name: "DictItemModel",
  components: {},
  data() {
    return {
      title: '',
      visible: false,
      loading: false,
      baseUrl: '/config/dict/item',
      editable: false,
      formdata: {},
      formrule: {
        // 表单校验
        code: [
          {required: true, message: "字典值不能为空", trigger: "blur"},
          {pattern: /^\w{2,30}$/, message: '组织编码只能包含数字、字母、下划线,长度在2-30个字符', trigger: 'blur'},
        ],
        name: [
          {required: true, message: "字典标签不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "字典状态不能为空", trigger: "blur"}
        ],
      },
      localDict: {
        system_status: [],
      },
    }
  },
  created() {
    this.getLocalDict();
  },
  methods: {
    add(type) {
      this.resetForm('form')
      this.formdata = {
        type: type,
        status: '00',
      }
      this.editable = false
      this.visible = true
      this.title = '新增字典'
    },
    edit(id) {
      this.resetForm('form')
      getOne(this.baseUrl, id).then(res => {
        this.title = '修改字典'
        this.visible = true
        this.editable = true
        this.formdata = res.data
      })
    },
    async getLocalDict() {
      await this.getStaticData('system-status').then(res => {
        this.localDict.system_status = res.data
      })
    },
    handleSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true
          save(this.baseUrl, this.formdata).then(() => {
            this.$modal.msgSuccess("保存成功");
            this.visible = false;
            this.resetForm('form')
            this.$emit('ok')
          }).finally(() => {
            this.loading = false
          });
        }
      });
    },
    handleCancel() {
      this.visible = false
      this.resetForm('form')
      this.$emit('close')
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
